package com.ruoyi.safe.controller.web;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.safe.domain.BizEnterTask;
import com.ruoyi.safe.domain.dto.BizEnterCheckDTO;
import com.ruoyi.safe.domain.dto.BizEnterDTO;
import com.ruoyi.safe.domain.dto.EnterCheckDTO;
import com.ruoyi.safe.service.IBizEnterCheckProjectService;
import com.ruoyi.safe.service.IBizEnterTaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.safe.domain.BizEnterCheck;
import com.ruoyi.safe.service.IBizEnterCheckService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公司检查Controller
 *
 * @author ruoyi
 * @date 2023-12-05
 */
@RestController
@RequestMapping("/enter/enter")
public class BizEnterCheckController extends BaseController
{
    @Autowired
    private IBizEnterCheckService bizEnterCheckService;
    @Autowired
    private IBizEnterTaskService bizEnterTaskService;

    /**
     * 查询公司检查列表
     */
    @PreAuthorize("@ss.hasPermi('enter:enter:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizEnterCheckDTO bizEnterCheckDTO)
    {
        List<BizEnterCheck> list = bizEnterCheckService.selectBizEnterCheckList(bizEnterCheckDTO,
                SecurityUtils.getLoginUser().getUser());
        return getDataTable(list);
    }

    /**
     * 根据企业检查id查询任务列表
     * @param id
     * @return
     */
    @GetMapping(value = "/getList/{checkId}")
    public TableDataInfo projectListByCheckId(@PathVariable("checkId") Long id){
        startPage();
        //根据公司检查id查询项目列表
//        List<BizEnterCheckProject> bizEnterCheckProjectList = bizEnterCheckProjectService.selectProjectListByCheckId(id);
        List<BizEnterTask> bizEnterTaskList = bizEnterTaskService.selectTaskListByCheckId(id);
        return getDataTable(bizEnterTaskList);
    }

    /**
     * 公司检查--我参与的检查
     * @param bizEnterCheck
     * @return
     */
    @PreAuthorize("@ss.hasPermi('enter:enter:list')")
    @GetMapping("/myList")
    public TableDataInfo myList(BizEnterCheck bizEnterCheck)
    {
        startPage();
        List<BizEnterCheck> list = bizEnterCheckService.selectBizEnterMyList(bizEnterCheck, SecurityUtils.getLoginUser().getUser());
        return getDataTable(list);
    }

    /**
     * 公司检查--我的待办
     * @param bizEnterCheckDTO
     * @return
     */
    @GetMapping("/myTodo")
    public TableDataInfo myTodo(BizEnterCheckDTO bizEnterCheckDTO){
        startPage();
        List<EnterCheckDTO> list = bizEnterCheckService.selectToDoList(bizEnterCheckDTO,
                SecurityUtils.getLoginUser().getUser());
        return getDataTable(list);
    }

    /**
     * 导出公司检查列表
     */
    @PreAuthorize("@ss.hasPermi('enter:enter:export')")
    @Log(title = "公司检查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizEnterCheckDTO bizEnterCheckDTO)
    {
        List<BizEnterCheck> list = bizEnterCheckService.selectBizEnterCheckList(bizEnterCheckDTO,
                SecurityUtils.getLoginUser().getUser());
        ExcelUtil<BizEnterCheck> util = new ExcelUtil<BizEnterCheck>(BizEnterCheck.class);
        util.exportExcel(response, list, "公司检查数据");
    }

    /**
     * 获取公司检查详细信息
     */
    @PreAuthorize("@ss.hasPermi('enter:enter:query')")
    @GetMapping(value = "/get/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizEnterCheckService.selectBizEnterCheckById(id));
    }

    /**
     * 新增公司检查
     */
    @PreAuthorize("@ss.hasPermi('enter:enter:add')")
    @Log(title = "公司检查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizEnterCheck bizEnterCheck)
    {
        return toAjax(bizEnterCheckService.insertBizEnterCheck(bizEnterCheck,
                SecurityUtils.getLoginUser().getUser()));
    }

    /**
     * 点击发送任务按钮时的新增
     * @param bizEnterDTO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('enter:enter:add')")
    @Log(title = "公司检查", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult addEnter(@RequestBody BizEnterDTO bizEnterDTO){
        return toAjax(bizEnterCheckService.insertEnter(bizEnterDTO,
                SecurityUtils.getLoginUser().getUser()));
    }

    /**
     * 修改公司检查
     */
    @PreAuthorize("@ss.hasPermi('enter:enter:edit')")
    @Log(title = "公司检查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizEnterCheck bizEnterCheck)
    {
        return toAjax(bizEnterCheckService.updateBizEnterCheck(bizEnterCheck,
                SecurityUtils.getLoginUser().getUser()));
    }

    /**
     * 删除公司检查
     */
    @PreAuthorize("@ss.hasPermi('enter:enter:remove')")
    @Log(title = "公司检查", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizEnterCheckService.deleteBizEnterCheckByIds(ids));
    }

    /**
     * 获取公司检查详细信息
     */
    @PreAuthorize("@ss.hasPermi('enter:enter:query')")
    @GetMapping(value = "/getDataPivot/{id}")
    public AjaxResult getDataPivot(@PathVariable("id") Long id)
    {
        return success(bizEnterCheckService.getDataPivotById(id));
    }
}
